
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
using LPV_POS;
using LPV_POS.mh_pos;
using LPV_POS.Module;
public partial class frmDM_SDBDKHO : Form
{
    public string ma = "";//PK_: giá trị này dc truyền từ bảng danh sách, trường hợp EDIT
    DataTable tbMainDS = new DataTable();
    DataTable tbMain = new DataTable();
    Boolean iloadform = false;
    Boolean them_sua = false;  //  false= them ; true= sua

    #region"    frmDM_SDBDKHO"
    public frmDM_SDBDKHO()
    {
        InitializeComponent();
    }
    #endregion

    #region"    frmDM_SDBDKHO_Load"
    public void frmDM_SDBDKHO_Load(object sender, EventArgs e)
    {
        btnluu.Enabled = false;
        btnxoa.Enabled = false;
        this.showcombobox();
        this.showdataDS();
        iloadform = true;


    }
    #endregion

    #region"    các hàm load dữ liệu"
    #region"    showdata"
    void showdata()
    {

        thuvien othuvien = new thuvien();//lớp truy xuat du lieu 
        tbMain = othuvien.SP_SelectDataTable("sp_DM_SDBDKHO",
                           new string[] { "@flag", "@ID" },
                           new object[] { 4, ma });

    }
    #endregion
    #region"    showdataDS"
    void showdataDS()
    {
        string[] param = new string[1];
        object[] value = new object[1];
        int ipos = 0;
        param[ipos] = "@flag";
        value[ipos++] = 4;
        

        grdDM_SDBDKHO.AutoGenerateColumns = false;

        thuvien othuvien = new thuvien();//lớp truy xuat du lieu 
        tbMainDS = othuvien.SP_SelectDataTable("sp_DM_SDBDKHO", param, value);
        grdDM_SDBDKHO.DataSource = tbMainDS;
        grdDM_SDBDKHO.Refresh();

    }

    private int f_update_sdbdkho(string makho, string mahh, decimal soluong, decimal giatri, int matonkho)
    {
        int ketqua=-1;
        string[] s = new string[] { "@flag", "@MAKHO", "@MAHH", "@SOLUONG", "@GIATRI", "@ID" };
        object[] o = new object[] { 2, makho,mahh, soluong, giatri,  matonkho };
        thuvien tv = new thuvien();
        ketqua = tv.SP_Update("sp_DM_SDBDKHO", s, o);
        return ketqua;
         
    }
    #endregion
    #region"    showinfo"
    void showinfo(int i1edt2sch)
    {
            this.showdata();
        if (tbMain.Rows.Count == 0)
        {
            numGIATRI.Value = 0;
            cboMAHH.Text = "";
            //cboMAKHO.Text = "";
            numSOLUONG.Value = 0;
            return;
        }
        numGIATRI.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["GIATRI"]).Replace("0-", "-"));
        cboMAHH._SetselectedID(tbMain.Rows[0]["MAHH"] + "");
        cboMAKHO._SetselectedID(tbMain.Rows[0]["MAKHO"] + "");
        numSOLUONG.Value = decimal.Parse(string.Concat("0" + tbMain.Rows[0]["SOLUONG"]).Replace("0-", "-"));

    }
    #endregion
    #region"    showcombobox"
    void showcombobox()
    {

        thuvien othuvien = new thuvien();
        DataTable tbDM_KHOHANG = othuvien.SP_SelectDataTable("sp_DM_SDBDKHO", new string[] { "@flag" }, new object[] { 5 });
        cboMAKHO.Datasource = tbDM_KHOHANG;
        cboMAKHO._Init();

        DataTable tbDM_HH = othuvien.SP_SelectDataTable("sp_DM_SDBDKHO", new string[] { "@flag" }, new object[] { 6 });
        cboMAHH.Datasource = tbDM_HH;
        cboMAHH._Init();


    }
    #endregion
    #endregion

    #region"    các sự kiện _Click của các nút THÊM - SỬA- TÌM KIẾM - THOÁT - tbpDanhSach"
    #region"    btnDSthem_Click"
    private void btnDSthem_Click(object sender, EventArgs e)
    {
        them_sua = false;
        tbMain = tbMainDS.Copy();
        tbMain.Clear();
        ma = "-1";
        this.showinfo(1);
        this.setStatusButton(1);
    }

    #endregion
    #region"    btnDSthoat_Click"
    private void btnDSthoat_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Bạn muốn thoát?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)
        {
            this.Dispose();
        }

    }
    #endregion
    #region"    btnDStimkiem_Click"
    private void btnDStimkiem_Click(object sender, EventArgs e)
    {
        iloadform = false;
        tbMain = tbMainDS.Copy();
        tbMain.Clear();
        ma = "-1";
        this.setStatusButton(3);
        this.showinfo(2);
        iloadform = true;

    }
    #endregion
    #endregion

    #region"    các sự kiện _Click của các nút LƯU - XÓA - SỬA  "
    #region"    tao_ma_tu_dong"
    void tao_ma_tu_dong()
    {
    }

    #endregion
    #region"    btnluu_Click"
    private void btnluu_Click(object sender, EventArgs e)
    {
        if (this.save() > 0)
        {
            MessageBox_LPV.Show("Lưu thành công");
            this.showdataDS();
            this.DialogResult = DialogResult.OK;
        }
    }

    #endregion
    #region"    save"
    int save()
    {
        int _iReasult = -1;

        if (them_sua == false)
        {
            if (this.ValidateSave())
            {
                thuvien othuvien = new thuvien();//lớp truy xuat du lieu
                _iReasult = othuvien.SP_Update("sp_DM_SDBDKHO",
                     new string[] { "@flag", "@ID", "@GIATRI", "@MAHH", "@MAKHO", "@SOLUONG" },
                     new object[] { 1, ma, numGIATRI.Value, cboMAHH._GetselectedID(), cboMAKHO._GetselectedID(), numSOLUONG.Value });
            }
        }
        else
        {
           
            string makho;
            string mahh;
            decimal soluong;
            decimal giatri;
            int matonkho;

            if (grdDM_SDBDKHO.Rows.Count > 0)
            {

                makho = cboMAKHO._GetselectedID();
                mahh = cboMAHH._GetselectedID() ;
                soluong = numSOLUONG.Value;
                matonkho = int.Parse(grdDM_SDBDKHO.CurrentRow.Cells["col_ma"].Value + "");
                giatri = numGIATRI.Value;

                _iReasult = f_update_sdbdkho(makho, mahh, soluong, giatri, matonkho);
              
            }
        }
   
        return _iReasult;
    }

    #endregion
    #region"    ValidateSave"
    Boolean ValidateSave()
    {
        return true;
    }

    #endregion
    #region"    delete"
    int delete()
    {
        thuvien othuvien = new thuvien();//lớp truy xuat du lieu    
        int _iReasult = othuvien.SP_Update("sp_DM_SDBDKHO",
                                 new string[] { "@flag", "@ID" },
                                 new object[] { 3, ma, });
        return _iReasult;
    }

    #endregion
    #region"    btnxoa_Click"
    private void btnxoa_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Bạn muốn xóa TỒN KHO BAN ĐẦU này?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
            if (this.delete() > 0)
            {
                MessageBox.Show("Xóa TỒN KHO BAN ĐẦU thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ma = "-1";
                this.showdataDS();
                this.setStatusButton(4);
            }
        }
    }

    #endregion
    #region"    sự kiên chọn trên lưới -- tblDanhSach"
    #region"    showSearch"
    void showSearch(object sender, EventArgs e)
    {
        if (sender != null)
        {
            tbMain = (DataTable)sender;
            grdDM_SDBDKHO.DataSource = tbMain;
            grdDM_SDBDKHO.Refresh();
        }
    }
    #endregion
    #region"    frmDM_SDBDKHO_KeyDown"
    private void frmDM_SDBDKHO_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.T)
        {
            this.btnDSthem_Click(null, null);
        }
        else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.K)
        {
            this.btnDStimkiem_Click(null, null);
        }
        else if (e.KeyCode == Keys.Escape)
        {
            this.btnDSthoat_Click(null, null);
        }
        else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.L)
        {
            this.btnluu_Click(null, null);
        }
        else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.X)
        {
            this.btnxoa_Click(null, null);
        }
    }
    #endregion
    #region"    grdDM_SDBDKHO_SelectionChanged"
    private void grdDM_SDBDKHO_SelectionChanged(object sender, EventArgs e)
    {

        if (iloadform && grdDM_SDBDKHO.CurrentRow != null)
        {
            if (grdDM_SDBDKHO.CurrentRow.Index >= 0)
            {
                iloadform = false;
                ma = "-1";
                string _iSTT = grdDM_SDBDKHO.Rows[grdDM_SDBDKHO.CurrentRow.Index].Cells[0].Value + "";
                DataRow[] _dr = tbMainDS.Select("STT='" + _iSTT + "'");
                if (_dr.Length > 0)
                {
                    ma = _dr[0]["ID"] + "";
                    this.setStatusButton(2);
                }
                this.showinfo(1);
                iloadform = true;
            }
        }
    }
    private void grdDM_SDBDKHO_DoubleClick(object sender, EventArgs e)
    {
        this.grdDM_SDBDKHO_SelectionChanged(null, null);
    }
    #endregion
    #endregion
    #region "    setStatusButton"
    void setStatusButton(int i1New2Edt3Sch4Del5Sav)
    {
        btnDSthem.Enabled = i1New2Edt3Sch4Del5Sav != 1;
        btnxoa.Enabled = (i1New2Edt3Sch4Del5Sav != 1 && i1New2Edt3Sch4Del5Sav != 4 && i1New2Edt3Sch4Del5Sav != 3);
        //btnDStimkiem.Enabled = i1New2Edt3Sch4Del5Sav != 3;
        btnluu.Enabled = i1New2Edt3Sch4Del5Sav != 5;
    }
    #endregion
    #region "  txt_KeyDown "
    private void txt_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            this.cbo_SelectedIndexChanged(null, null);
    }
    #endregion
    #region "  cbo_SelectionChanged "
    private void cbo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (iloadform)
        {
            
                iloadform = false;
                this.showdataDS();
                iloadform = true;
           
        }
    }
    #endregion
    #region"    grdDM_SDBDKHO_DataError"
    private void grdDM_SDBDKHO_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion
    #region"    grd_DataError"
    private void grd_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion

    private void btnSoLuong_Click(object sender, EventArgs e)
    {
        mh_so omh_so = new mh_so(lblSOLUONG.Text, (int)numSOLUONG.Value);
        int iso = omh_so.ShowForm();
        if (omh_so.DialogResult == DialogResult.OK)
            numSOLUONG.Value = iso;
    }
    #endregion

    private void btnGiaTri_Click(object sender, EventArgs e)
    {
        mh_so omh_so = new mh_so(lblGIATRI.Text, (int)numGIATRI.Value);
        int iso = omh_so.ShowForm();
        if (omh_so.DialogResult == DialogResult.OK)
            numGIATRI.Value = iso;
    }

    private void grdDM_SDBDKHO_Click(object sender, EventArgs e)
    {
        if (tbMainDS.Rows.Count <= 1)
            this.grdDM_SDBDKHO_SelectionChanged(null, null);
    }

    private void bton_sua_Click(object sender, EventArgs e)
    {
        them_sua = true;
         
    }


}

